/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.snda.dw.hiveapi.hiveapitest;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/**
 *
 * @author lidejia
 */
public class HiveSessionPool {

    private static HiveSessionPool instance;

    public static synchronized HiveSessionPool getInstance() {
        if (instance == null) {
            instance = new HiveSessionPool();
        }
        return instance;
    }
    //
    private Map<Long, HiveSession> sessions = new ConcurrentHashMap<Long, HiveSession>();
    private AtomicLong sessionIDCounter = new AtomicLong(0);

    private HiveSessionPool() {
    }

    public Long generateSessionID() {
        Long sessionID = sessionIDCounter.incrementAndGet();
        return sessionID;
    }

    public void addSession(HiveSession session) {
        sessions.put(session.getSessionID(), session);
    }

    public void releaseSession(HiveSession session) {
        sessions.remove(session.getSessionID());
    }
}
